package com.yulebaby.callsenter.adapter;


import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yulebaby.callsenter.R;
import com.yulebaby.callsenter.bean.CallLogBean;
import com.yulebaby.callsenter.global.App;
import com.yulebaby.callsenter.holder.MyViewHolder;
import com.yulebaby.callsenter.http.callback.OnItemClickListenerComm;
import com.yulebaby.callsenter.utils.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用adapter
 *
 * @author YK
 * @date 2017-1-7.
 */

public abstract class CommonAdapter<T> extends BaseQuickAdapter<T, MyViewHolder> {
    protected CommonAdapter commonAdapter;
    protected Context mContext;
    protected int mLayoutId;
    protected List<T> mDatas;
    protected LayoutInflater mInflater;
    private SparseBooleanArray selectedItems = new SparseBooleanArray();//记录选中的position
    private OnItemClickListenerComm mOnItemClickListenerComm;

    public void setOnItemClickListener(OnItemClickListenerComm onItemClickListenerComm) {
        this.mOnItemClickListenerComm = onItemClickListenerComm;
    }

    public CommonAdapter(Context context, int layoutId, List<T> datas) {
        super(layoutId, datas);
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mLayoutId = layoutId;
        mDatas = datas;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder viewHolder = MyViewHolder.get(mContext, null, parent, mLayoutId, -1);
        setListener(parent, viewHolder, viewType);

        return viewHolder;
    }

    protected void setListener(final ViewGroup parent, final MyViewHolder viewHolder, int viewType) {
        if (!isEnabled(viewType)) return;
        viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListenerComm != null && commonAdapter != null) {
                    int position = getPosition(viewHolder);
//                    mOnItemClickListenerComm.onItemClick(parent, v, mDatas.get(position), position);
                    mOnItemClickListenerComm.onItemClick(parent, v, commonAdapter, position);
                }
            }
        });

        viewHolder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemClickListenerComm != null && commonAdapter != null) {
                    int position = getPosition(viewHolder);
                    return mOnItemClickListenerComm.onItemLongClick(parent, v, mDatas.get(position), position);
//                    return mOnItemClickListenerComm.onItemLongClick(parent, v, commonAdapter, position);
                }
                return false;
            }
        });
    }


    protected int getPosition(MyViewHolder viewHolder) {
        return viewHolder.getAdapterPosition();
    }

    protected boolean isEnabled(int viewType) {
        return true;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.updatePosition(position);
        convert(holder, mDatas.get(position));
//        switch (((CallLogBean) mDatas.get(position)).getPlayState()) {
//            case 0:
//                ImageLoader.load(App.mContext, R.drawable.phone_fail, holder.getView(R.id.iv_play));
//                break;
//            case 1:
//                ImageLoader.load(App.mContext, R.drawable.phone_pause, holder.getView(R.id.iv_play));
//                break;
//            case 2:
//                ImageLoader.load(App.mContext, R.drawable.phone_success, holder.getView(R.id.iv_play));
//                break;
//        }
    }

    public T getItem(int position) {
        return this.mDatas.get(position);
    }

    public abstract void convert(MyViewHolder holder, T t);

    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    /**
     * 判读是否选中
     */
    public boolean isSelected(int position) {
        return getSelectedItems().contains(position);
    }

    /**
     * 切换选中或取消选中
     */
    public void switchSelectedState(int position) {
        if (selectedItems.get(position, false)) {
            selectedItems.delete(position);
        } else {
            selectedItems.put(position, true);
        }
        notifyItemChanged(position);
    }

    /**
     * 清除所有选中item的标记
     */
    public void clearSelectedState() {
        List<Integer> selection = getSelectedItems();
        selectedItems.clear();
        for (Integer i : selection) {
            notifyItemChanged(i);
        }
    }

    /**
     * 全选
     */
    public void selectAllItems() {
        clearSelectedState();
        for (int i = 0; i < getItemCount(); i++) {
            selectedItems.put(i, true);
            notifyItemChanged(i);
        }
    }

    /**
     * 获取item数目
     */
    public int getSelectedItemCount() {
        return selectedItems.size();
    }

    /**
     * 获取选中的items
     */
    public List<Integer> getSelectedItems() {
        List<Integer> items = new ArrayList<>(selectedItems.size());
        for (int i = 0; i < selectedItems.size(); i++) {
            items.add(selectedItems.keyAt(i));
        }
        return items;
    }
}
